/*
 * 邀请好友
 * page邀请好友
 */
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/my/inviteFriends.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:pin_input_text_field/pin_input_text_field.dart';
import 'package:qgchat/http/models/ReflectRecordModel.dart';
import 'package:qgchat/http/models/userInfoModel.dart';
import 'package:qgchat/states/states_model/index.dart';
import 'package:qgchat/widgets/share.dart';

class InviteFriends extends StatefulWidget {
  InviteFriends({Key key}) : super(key: key);

  @override
  IinviteFriendsState createState() => IinviteFriendsState();
}

class IinviteFriendsState extends State<InviteFriends> {
  InviteFriendsBloc bloc;
  @override
  Widget build(BuildContext context) {
    UserState user = state(context, 'user');
    return BlocProvider(
      bloc: InviteFriendsBloc(user.info.memberToken),
      child: Builder(
        builder: (context) {
          bloc = BlocProvider.of<InviteFriendsBloc>(context);
          return Scaffold(
            appBar: PreferredSize(
              preferredSize: Size.fromHeight(px(88)),
              child: AppBar(
                centerTitle: true,
                title: Text(
                  "邀请好友",
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: px(34),
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
            body: Container(
              color: col("6f3cab"),
              child: ListView(
                padding: EdgeInsets.only(bottom: px(80)),
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage("assets/images/invitebg.jpg"),
                        fit: BoxFit.contain,
                        alignment: Alignment.topCenter,
                      ),
                    ),
                    child: Column(
                      children: <Widget>[
                        percentage(),
                        invitationCode(),
                        myInvite(),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  // 分成百分比
  Widget percentage() {
    return Container(
      width: px(578),
      height: px(60),
      margin: EdgeInsets.only(top: px(347)),
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(px(30)),
        gradient: LinearGradient(colors: [
          col("2becdd"),
          col("24d4f2"),
          col("5a93fe"),
        ]),
      ),
      child: Text(
        "可获得被邀请人充值金额的10%作为分成",
        style: TextStyle(
          color: Colors.white,
          fontSize: px(28),
        ),
      ),
    );
  }

  // 邀请码
  Widget invitationCode() {
    UserInfoModel info = (state(context, 'user').info as UserInfoModel);

    return Container(
      width: px(704),
      height: px(970),
      margin: EdgeInsets.only(top: px(595)),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/images/invite-code-bg.png"),
          fit: BoxFit.cover,
          alignment: Alignment.topCenter,
        ),
      ),
      child: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(top: px(180)),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: px(60),
                  height: px(40),
                  margin: EdgeInsets.only(right: px(22)),
                ),
                Container(
                  width: px(431),
                  child: PinInputTextField(
                    enabled: false,
                    pinLength: 6,
                    keyboardType: TextInputType.text,
                    decoration: UnderlineDecoration(
                      color: col("eff1f4"),
                      enteredColor: col("494a4c"),
                      textStyle: TextStyle(
                        color: col("5640c6"),
                        fontWeight: FontWeight.bold,
                        fontSize: px(70),
                      ),
                    ),
                    controller: TextEditingController.fromValue(
                      TextEditingValue(
                        text: info.invitationcode,
                      ),
                    ),
                    autoFocus: false,
                    textInputAction: TextInputAction.go,
                    onSubmit: (pin) {
                      debugPrint('submit pin:$pin');
                    },
                  ),
                ),
                Container(
                  width: px(60),
                  height: px(40),
                  margin: EdgeInsets.only(left: px(22)),
                  child: FlatButton(
                    onPressed: () {
                      Clipboard.setData(
                        ClipboardData(text: info.invitationcode),
                      ).then((res) {
                        showToast("复制成功");
                      });
                    },
                    color: col("edeff2"),
                    padding: EdgeInsets.zero,
                    child: Text(
                      "复制",
                      style: TextStyle(
                        color: col("606266"),
                        fontSize: px(22),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            width: px(552),
            height: px(90),
            margin: EdgeInsets.only(top: px(60)),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(px(45)),
              gradient: LinearGradient(
                colors: [
                  col("a56dff"),
                  col("7759ff"),
                ],
              ),
            ),
            child: FlatButton(
              onPressed: () {
                ShareDialog.show(context);
              },
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(
                  sp(45),
                ),
              ),
              child: Text(
                "分享邀请赚现金",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: px(38),
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: px(240)),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                stateBuilder(context, 'user', (UserState user) {
                  if (user.info.otherinvitationcode != null) {
                    return Container(
                      width: px(431),
                      child: PinInputTextField(
                        enabled: false,
                        controller: TextEditingController.fromValue(
                          TextEditingValue(
                            text: user.info.otherinvitationcode,
                          ),
                        ),
                        keyboardType: TextInputType.text,
                        inputFormatter: [],
                        pinLength: 6,
                        decoration: UnderlineDecoration(
                          color: col("eff1f4"),
                          enteredColor: col("494a4c"),
                          textStyle: TextStyle(
                            color: col("5640c6"),
                            fontWeight: FontWeight.bold,
                            fontSize: px(70),
                          ),
                        ),
                        autoFocus: false,
                        textInputAction: TextInputAction.go,
                      ),
                    );
                  } else {
                    return Container(
                      width: px(431),
                      child: PinInputTextField(
                        controller: bloc.codeC,
                        keyboardType: TextInputType.text,
                        inputFormatter: [],
                        pinLength: 6,
                        decoration: UnderlineDecoration(
                          color: col("eff1f4"),
                          enteredColor: col("494a4c"),
                          textStyle: TextStyle(
                            color: col("5640c6"),
                            fontWeight: FontWeight.bold,
                            fontSize: px(70),
                          ),
                        ),
                        autoFocus: false,
                        textInputAction: TextInputAction.go,
                        onSubmit: (pin) {
                          debugPrint('submit pin:$pin');
                        },
                      ),
                    );
                  }
                }),
              ],
            ),
          ),
          Container(
            width: px(240),
            height: px(86),
            margin: EdgeInsets.only(top: px(50)),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(px(43)),
              gradient: LinearGradient(
                colors: [
                  col("ffe48d"),
                  col("ffcf3d"),
                ],
              ),
            ),
            child: FlatButton(
              onPressed: () {
                if (info.otherinvitationcode == null) {
                  bloc.sharehref(context);
                } else {
                  showToast("你已填写邀请码");
                }
              },
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(
                  sp(43),
                ),
              ),
              child: Text(
                "提交",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: px(34),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  // 我的邀请
  Widget myInvite() {
    return Container(
      margin: EdgeInsets.only(top: px(50)),
      child: Column(
        children: <Widget>[
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  "assets/images/icon/invite.png",
                  width: px(24),
                  height: px(37),
                ),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: px(20)),
                  child: Text(
                    "我的邀请",
                    style: TextStyle(
                      color: col("fcca33"),
                      fontSize: px(38),
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Image.asset(
                  "assets/images/icon/invite.png",
                  width: px(24),
                  height: px(37),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: px(25)),
            child: StreamBuilder(
              stream: bloc.reflectRecordStream,
              initialData: bloc.tomalsum,
              builder: (
                BuildContext context,
                AsyncSnapshot snapshot,
              ) {
                ReflectRecordModel count = snapshot.data;
                return RichText(
                  text: TextSpan(
                    text: "您已累计获得奖励",
                    style: TextStyle(
                      color: col("ffffff"),
                      fontSize: px(32),
                    ),
                    children: <TextSpan>[
                      TextSpan(
                        text: " ${count.tomalsum} ",
                        style: TextStyle(
                          color: col("fcca33"),
                        ),
                      ),
                      TextSpan(
                        text: "元",
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: px(60)),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: px(335),
                  height: px(250),
                  margin: EdgeInsets.symmetric(horizontal: px(10)),
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage("assets/images/invite-my-bg.png"),
                      fit: BoxFit.contain,
                    ),
                    boxShadow: [
                      BoxShadow(
                        color: Color.fromRGBO(84, 34, 143, 0.68),
                        offset: Offset(3.0, 3.0),
                        blurRadius: 10.0,
                        spreadRadius: 2.0,
                      ),
                    ],
                  ),
                  child: Column(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(top: px(30)),
                        child: StreamBuilder(
                          stream: bloc.intodetailsStream,
                          initialData: [],
                          builder:
                              (BuildContext context, AsyncSnapshot snapshot) {
                            return Text(
                              "${snapshot.data.length}",
                              style: TextStyle(
                                height: .95,
                                color: col("5640c6"),
                                fontSize: px(54),
                                fontWeight: FontWeight.bold,
                              ),
                            );
                          },
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: px(15)),
                        child: Text(
                          "成功邀请 (人)",
                          style: TextStyle(
                            height: .95,
                            color: col("909399"),
                            fontSize: px(26),
                          ),
                        ),
                      ),
                      Container(
                        width: px(198),
                        height: px(70),
                        margin: EdgeInsets.only(top: px(35)),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(px(35)),
                          gradient: LinearGradient(
                            colors: [
                              col("ac70ff"),
                              col("6f56ff"),
                            ],
                          ),
                        ),
                        child: FlatButton(
                          onPressed: () {
                            goPage(context, "/inviteInfo");
                          },
                          padding: EdgeInsets.zero,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(px(35)),
                          ),
                          child: Text(
                            "邀请详情",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: px(28),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  width: px(335),
                  height: px(250),
                  margin: EdgeInsets.symmetric(horizontal: px(10)),
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage("assets/images/invite-my-bg.png"),
                      fit: BoxFit.contain,
                    ),
                    boxShadow: [
                      BoxShadow(
                        color: Color.fromRGBO(84, 34, 143, 0.68),
                        offset: Offset(3.0, 3.0),
                        blurRadius: 10.0,
                        spreadRadius: 2.0,
                      ),
                    ],
                  ),
                  child: Column(
                    children: <Widget>[
                      StreamBuilder(
                        stream: bloc.reflectRecordStream,
                        initialData: bloc.tomalsum,
                        builder: (
                          BuildContext context,
                          AsyncSnapshot snapshot,
                        ) {
                          ReflectRecordModel count = snapshot.data;
                          return Container(
                            margin: EdgeInsets.only(top: px(30)),
                            child: Text(
                              "${count.tomalsum}",
                              style: TextStyle(
                                height: .95,
                                color: col("5640c6"),
                                fontSize: px(54),
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          );
                        },
                      ),
                      Container(
                        margin: EdgeInsets.only(top: px(15)),
                        child: Text(
                          "累计获得 (元)",
                          style: TextStyle(
                            height: .95,
                            color: col("909399"),
                            fontSize: px(26),
                          ),
                        ),
                      ),
                      Container(
                        width: px(198),
                        height: px(70),
                        margin: EdgeInsets.only(top: px(35)),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(px(35)),
                          gradient: LinearGradient(
                            colors: [
                              col("ac70ff"),
                              col("6f56ff"),
                            ],
                          ),
                        ),
                        child: FlatButton(
                          onPressed: () =>
                              goPage(context, "/cashOut")?.then((est) {
                            UserState user = state(context, 'user');
                            bloc.reflectRecord(user.info.memberToken);
                          }),
                          padding: EdgeInsets.zero,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(px(35)),
                          ),
                          child: Text(
                            "申请提现",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: px(28),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
